package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelFileResourceHandleVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FileHandleRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FileResourceHandleExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelFileResourceHandle;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FileResourceHandleDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
　　* @description: 文件操作数据持久化实现
　　* @author shenrm
　　* @date 2021/11/21 14:40
　　*/
@Repository
public class FileHandleRepositoryImpl implements FileHandleRepository {

    @Autowired
    private FileResourceHandleDAO fileResourceHandleDAO;

    @Override
    public int batchInsertFileResources(List<FlowModelFileResourceHandleVo> handleVos) {
        if(CollectionUtils.isNotEmpty(handleVos)) {
            List<FlowModelFileResourceHandle> fileResourceList = new ArrayList<FlowModelFileResourceHandle>(handleVos.size());
            for (FlowModelFileResourceHandleVo handleVo : handleVos){
                FlowModelFileResourceHandle fileResourceHandle = new FlowModelFileResourceHandle();
                BeanCopierUtils.copyProperties(handleVo, fileResourceHandle);
                fileResourceList.add(fileResourceHandle);
            }
            // 批量新增
            fileResourceHandleDAO.batchInsert(fileResourceList);
        }
        return 0;
    }

    @Override
    public List<FlowModelFileResourceHandleVo> selectByCondition(FlowModelFileResourceHandleVo handleRelVo) {
        return this.convert(fileResourceHandleDAO.selectByCondition(handleRelVo.getDeploymentId(), handleRelVo.getActivityId(), handleRelVo.getServiceType()));
    }

    @Override
    public void updateByExampleSelective(FlowModelFileResourceHandleVo handleRelVo) {
        FlowModelFileResourceHandle handleRel = new FlowModelFileResourceHandle();
        // 更新部署ID
        handleRel.setDeploymentId(handleRelVo.getDeploymentId());
        // 更新节点编码
        if(StringUtils.isNotBlank(handleRelVo.getActivityId())) {
            handleRel.setActivityId(handleRelVo.getActivityId());
        }
        // 更新文件名称
        if(StringUtils.isNotBlank(handleRelVo.getFileName())) {
            handleRel.setFileName(handleRelVo.getFileName());
        }
        // 创建更新条件
        FileResourceHandleExample updateCondition = new FileResourceHandleExample();
        FileResourceHandleExample.Criteria criteria = updateCondition.createCriteria();
        // 附件ID唯一标识
        criteria.andAttachIdEqualTo(handleRelVo.getAttachId());
        // 业务类型,0:全局;1:节点
        if(StringUtils.isNotBlank(handleRelVo.getServiceType())) {
            criteria.andServiceTypeEqualTo(handleRelVo.getServiceType());
        }
        fileResourceHandleDAO.updateByExampleSelective(handleRel, updateCondition);
    }

    private List<FlowModelFileResourceHandleVo> getPageableResult(List<FlowModelFileResourceHandle> handleRelList) {
        PageInfo<FlowModelFileResourceHandle> pageInfo = new PageInfo<>(handleRelList);
        Page<FlowModelFileResourceHandleVo> result = null;
        try {
            result = new Page<>();
            result.setPageNum(pageInfo.getPageNum()).setPageSize(pageInfo.getPageSize()).addAll(convert(handleRelList));
            result.setTotal(pageInfo.getTotal());
            return result;
        } finally {
            if (Objects.nonNull(result)) {
                result.close();
            }
        }
    }

    private List<FlowModelFileResourceHandleVo> convert(List<FlowModelFileResourceHandle> sources) {
        if(CollectionUtils.isEmpty(sources)) {
            return Collections.emptyList();
        }

        // 循环翻译标题
        List<FlowModelFileResourceHandleVo> handleRelVos = sources.stream().map(item -> {
            FlowModelFileResourceHandleVo tmpVo = new FlowModelFileResourceHandleVo();
            BeanUtils.copyProperties(item, tmpVo);
            return tmpVo;
        }).collect(Collectors.toList());
        return handleRelVos;
    }

    /**
     * 根据附件ID集合条件查询
     *
     * @param attachIds
     * @return
     */
    @Override
    public List<FlowModelFileResourceHandleVo> selectByAttachIds(List<String> attachIds) {
        // 创建查询条件对象
        FileResourceHandleExample queryCondition = new FileResourceHandleExample();

        // 添加条件
        FileResourceHandleExample.Criteria criteria = queryCondition.createCriteria();

        // 附件ID集合
        if(CollectionUtils.isNotEmpty(attachIds)) {
            criteria.andAttachIdIn(attachIds);
        }
        return this.convert(fileResourceHandleDAO.selectByExample(queryCondition));
    }

}
